package com.lujieni.msg.provider.controller;


import com.alibaba.fastjson.JSON;
import com.lujieni.msg.provider.dao.LocalEventDao;
import com.lujieni.msg.provider.dao.StudentDao;
import com.lujieni.msg.provider.dto.StudentDTO;
import com.lujieni.msg.provider.po.LocalEventPO;
import com.lujieni.msg.provider.po.StudentPO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private LocalEventDao localEventDao;

    @Autowired
    private StudentDao studentDao;

    @PostMapping("/insertWithLocalMessage")
    @Transactional
    public String insertWithLocalMessage(@RequestBody StudentDTO dto){
        StudentPO po = new StudentPO();
        BeanUtils.copyProperties(dto, po);
        po.setActive(1);
        po.setCreateUserCode("181268");
        po.setModifyUserCode("181268");
        po.setCreateTime(new Date());
        po.setModifyTime(new Date());
        studentDao.insert(po);
        LocalEventPO localEventPO = new LocalEventPO();
        localEventPO.setType(1);
        localEventPO.setStatus(0); //待发送
        localEventPO.setContent(JSON.toJSONString(dto));
        localEventPO.setCreateUserCode("181268");
        localEventPO.setModifyUserCode("181268");
        localEventPO.setCreateTime(new Date());
        localEventPO.setModifyTime(new Date());
        localEventPO.setActive(1);
        localEventDao.insert(localEventPO);
        return "success";
    }

}
